<?php
function smarty_modifier_tdhescape($s)
{
    $s = preg_replace('/&#(\d{1,8});/i', '{|{$1}|}', $s);
    $s = htmlspecialchars($s, ENT_QUOTES);
    $s = preg_replace('/\{\|\{(\d{1,8})\}\|\}/i', '&#$1;', $s);
    return $s;
}

?>